﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Windows;

namespace Lolitemchanger
{
    class CGestionItem
    {
        List<CItem> m_CharacterListItem = new List<CItem>();
        List<CItem> m_AllItemList = new List<CItem>();
        CGestionFichierBinaire m_BinFile = new CGestionFichierBinaire();
        string m_strLanguage = null;
        string m_strCurrentCharacter = null;
        string m_strFilePath = null;
        string m_strFinalPath = null;

        public CGestionItem(string language)
        {
            m_strLanguage = language;
        }
        public void Reset()
        {
            m_BinFile.Reset();
            m_CharacterListItem.Clear();
            m_AllItemList.Clear();
            m_strCurrentCharacter = null;
            m_strCurrentCharacter = null;
            m_strLanguage = null;
            m_strFinalPath = null;
        }
        private void SetFinalPath()
        {
            if(m_strCurrentCharacter != null && m_strFilePath != null)
                m_BinFile.SetFileName(m_strFilePath + @"\" + m_strCurrentCharacter + @"\" + m_strCurrentCharacter + ".inibin");
        }
        public void SetCurrentCharacter(string character)
        {
            m_strCurrentCharacter = character;
            SetFinalPath();
        }
        public string GetCurrentCharacter()
        {
            return m_strCurrentCharacter;
        }
        public void SetFilePath(string path)
        {
            m_strFilePath = path;
            SetFinalPath();
        }
        public string GetFilePath()
        {
            return m_strFilePath;
        }
        public List<CItem> UpdateCharacterItem()
        {
            try
            {
                if (m_strFilePath != null && m_strCurrentCharacter != null && m_strLanguage != null)
                {

                    if (m_BinFile.WriteRequired())
                    {
                        m_BinFile.WriteFile();
                    }

                    m_BinFile.ReadFile();


                    if (m_BinFile.IsItemReady())
                    {
                        CGestionIni IniFile = new CGestionIni(Environment.CurrentDirectory + "\\Item.ini");
                        if (IniFile.Exist())
                        {
                            m_CharacterListItem.Add(new CItem(m_BinFile.GetItemNumber(0), IniFile.RecupererValeur(m_strLanguage, m_BinFile.GetItemNumber(0))));
                            m_CharacterListItem.Add(new CItem(m_BinFile.GetItemNumber(1), IniFile.RecupererValeur(m_strLanguage, m_BinFile.GetItemNumber(1))));
                            m_CharacterListItem.Add(new CItem(m_BinFile.GetItemNumber(2), IniFile.RecupererValeur(m_strLanguage, m_BinFile.GetItemNumber(2))));
                            m_CharacterListItem.Add(new CItem(m_BinFile.GetItemNumber(3), IniFile.RecupererValeur(m_strLanguage, m_BinFile.GetItemNumber(3))));
                            m_CharacterListItem.Add(new CItem(m_BinFile.GetItemNumber(4), IniFile.RecupererValeur(m_strLanguage, m_BinFile.GetItemNumber(4))));
                            m_CharacterListItem.Add(new CItem(m_BinFile.GetItemNumber(5), IniFile.RecupererValeur(m_strLanguage, m_BinFile.GetItemNumber(5))));
                            return m_CharacterListItem;
                        }
                    }
                }
                return null;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
                return null;
            }
        }
        public List<CItem> GetAllItem()
        {


                StreamReader sr = null;
                string line;
                bool readOK = false;

                try
                {
                    if (File.Exists(Environment.CurrentDirectory + "\\Item.ini") && m_strLanguage !=null)
                    {
                        m_AllItemList.Clear();
                        sr = new StreamReader(Environment.CurrentDirectory + "\\Item.ini");
                        line = sr.ReadLine();
                        while (line != null)
                        {
                            string[] split = line.Split('=');
                            if (split.Count() == 1)
                            {
                                if (split[0] == "["+m_strLanguage+"]")
                                    readOK = true;
                                else
                                    readOK = false;
                            }
                            else if(split.Count() == 2 && readOK)
                            {
                                m_AllItemList.Add(new CItem(split[0],split[1]));
                            }
                            line = sr.ReadLine();
                        }

                    }
                    if (sr != null) sr.Close();
                    return m_AllItemList;
                    
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    if (sr != null) sr.Close();
                    return null;
                }
        }
        public void EditCharacterItem(int index,string number,string name)
        {
            if (m_CharacterListItem.Count == 6 && index <=5 && index >=0)
            {
                m_CharacterListItem[index].SetName(name);
                m_CharacterListItem[index].SetNumber(number);
                m_BinFile.EditItem(index, number);
            }
        }
        public void SetLanguage(string language)
        {
            m_strLanguage = language;
        }
        public string GetLanguage()
        {
            return m_strLanguage;
        }
    }
    class CItem
    {
        string m_strNumber = null;
        string m_strName = null;

        public CItem(string number,string name)
        {
            m_strNumber = number;
            m_strName = name;
        }

        public string GetNumber()
        {
            return m_strNumber;
        }
        public void SetNumber(string number)
        {
            m_strNumber = number;
        }
        public string GetName()
        {
            return m_strName;
        }
        public void SetName(string name)
        {
            m_strName = name;
        }
    }
}
